package utils
{
	public class NumbertoWords
	{
		private var retvalue:String;
		private var a:Array=["","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE",];
		private var b:Array=["HUNDRED","THOUSAND","LAKH","CRORE"];
		private var c:Array=["TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINTEEN",];
		private var d:Array=["TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINTY"];
		
		public function NumbertoWords()
		{
			
		}
		public function convertWords(count:int):String {
			var i:int=1;
			var num:int;
			retvalue="";
			if(count < 100000000)
			{
				while (count != 0){
					switch (i){
						case 1:
							num = count % 100;
							passString(num);
							if( count > 100&& count % 100!= 0){
								displayOutput("AND ") ;
							}
							count /= 100;
							break ;
						case 2:
							num = count % 10;
							if ( num != 0){
								displayOutput(" ") ;
								displayOutput(b[0]);
								displayOutput(" ");
								passString(num);
							}
							count /= 10;
							break ;
						
						case 3:
							num = count % 100;
							if ( num != 0){
								displayOutput(" ") ;
								displayOutput(b[1]) ;
								displayOutput(" ") ;
								passString(num) ;
							}
							count /= 100;
							break ;
						case 4:
							num = count % 100;
							if (num != 0){
								displayOutput(" ");
								displayOutput(b[2]);
								displayOutput(" ");
								passString(num) ;
							}
							count /= 100;
							break ;
						
						case 5:
							num = count % 100;
							if (num != 0){
								displayOutput(" ") ;
								displayOutput(b[3]) ;
								displayOutput(" ") ;
								passString(num) ;
							}
							count /= 100;
							break ;
						
					}
					i++ ;
				}
			}
			else
			{
				retvalue = "Number is too Large";
			}
			return retvalue;
		}
		public function passString(inNum:int):void {
			var num:int; 
			var q:int; 
			if (inNum < 10){
				displayOutput(a[inNum]) ;
			}
			if (inNum > 9&& inNum < 20){
				displayOutput(c[inNum-10]) ;
			}
			if ( inNum > 19){
				num = inNum % 10;
				if ( num == 0){
					q = inNum / 10;
					displayOutput (d[q-2]) ;
				}
				else{
					q = inNum / 10;
					displayOutput(a[num]);
					displayOutput(" ") ;
					displayOutput(d[q-2]);
				}
			}
		}
		
		public function displayOutput(s:String):void {
			var t:String;
			t= retvalue;
			retvalue= s ;
			retvalue+= t ;
		}
	}
}